package com.easyattendance.services;

import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.easyattendance.model.Assignment;
import com.easyattendance.model.AssignmentStudent;
import com.easyattendance.model.StudentAttendance;
import com.easyattendance.util.HibernateUtil;

public class AssignmentSubmissionService {

	/* Method to CREATE assignment submission records in the database */
	   public synchronized boolean saveAssignmentSubmission(AssignmentStudent assignment_stud){

		  Session session = HibernateUtil.openSession();
	      Transaction tx = null;
	      boolean result=false;
	      
	      try{
	         tx = session.beginTransaction();
	         
	         
	        	 session.save(assignment_stud);
	           	 tx.commit();
	          
	         result=true;
	          
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
	      return result;
	   }
	   
	   /* Method to update assignment submission records in the database */
	   public synchronized boolean updateAssignmentSubmission(AssignmentStudent assignment_stud){

		  Session session = HibernateUtil.openSession();
	      Transaction tx = null;
	      boolean result=false;
	      
	      try{
	         tx = session.beginTransaction();
	         
	         
	        	 session.update(assignment_stud);
	           	 tx.commit();
	          
	         result=true;
	          
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
	      return result;
	   }
	   
	   
	   
	   public boolean ifAlreadyExists(AssignmentStudent newStudentAssignment) {
			boolean flag = false;

			Session session = HibernateUtil.openSession();
			Transaction tx = null;
			List stud_assignments = null;
			try {
				tx = session.beginTransaction();
				stud_assignments = session.createQuery("FROM AssignmentStudent").list();
				for (Iterator iterator = stud_assignments.iterator(); iterator.hasNext();) {
					AssignmentStudent stud_assign = (AssignmentStudent) iterator.next();
					if (stud_assign.equals(newStudentAssignment)) {
						System.out.println("This record already exists");
						flag = true;
						break;
					}

				}
				tx.commit();
			} catch (HibernateException e) {
				if (tx != null)
					tx.rollback();
				e.printStackTrace();
			} finally {
				session.close();
			}

			return flag;

		}
	   
	   /* Method to  READ all the Assignment submission records */
	   public List listAssignmentsSubmitted( ) {
		   Session session = HibernateUtil.openSession();
	      Transaction tx = null;
	      List submitted_assignment = null;
	      try{
	         tx = session.beginTransaction();
	         submitted_assignment = session.createQuery("FROM AssignmentStudent").list(); 
	         for (Iterator iterator = submitted_assignment.iterator(); iterator.hasNext();){
	        	 AssignmentStudent ass_stud = (AssignmentStudent) iterator.next(); 
	             
	         }
	         tx.commit();
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
	      return submitted_assignment;
	   }
	   
	   /* Method to  READ all the Assignment submission records as per class,subject and assignment id*/
	   public List listAssignmentsSubmittedByClass(String classId,String subjectId,String assignmentId) {
		   Session session = HibernateUtil.openSession();
	      Transaction tx = null;
	      List submitted_assignment = null;
	      try{
	         tx = session.beginTransaction();
	         submitted_assignment = session.createQuery("FROM AssignmentStudent where class_id='"+classId+"' and subject_id='"+subjectId+"' and assignment_id='"+assignmentId+"'").list(); 
	         for (Iterator iterator = submitted_assignment.iterator(); iterator.hasNext();){
	        	 AssignmentStudent ass_stud = (AssignmentStudent) iterator.next(); 
	             
	         }
	         tx.commit();
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
	      return submitted_assignment;
	   }
	   
	   /* Method to  READ subject-wise Assignment submission records of students*/
	   public int listSubjectwiseAssignments(String classId,String subjectId,String teacherId) {
		   Session session = HibernateUtil.openSession();
	      Transaction tx = null;
	      List assignment = null;
	      try{
	         tx = session.beginTransaction();
	         assignment = session.createQuery("FROM Assignment where class_id='"+classId+"' and subject_id='"+subjectId+"' and teacher_id='"+teacherId+"'").list(); 
	        
	         tx.commit();
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
	      return assignment.size();
	   }
	   
	   /* Method to  READ the Assignments submitted by students for particular subject*/
	   public List<AssignmentStudent> listSubjectwiseAssignmentSubmitted(String classId,String subjectId,int stud_roll_no) { 
		   Session session = HibernateUtil.openSession();
	      Transaction tx = null;
	      List<AssignmentStudent> submitted_assignment = null;
	      try{
	         tx = session.beginTransaction();
	         submitted_assignment = session.createQuery("FROM AssignmentStudent as ass_stud where class_id="
	     		+ "'"+classId+"' and subject_id='"+subjectId+"' and stud_roll_no="+stud_roll_no+"").list(); 
	        /* for (Iterator iterator = submitted_assignment.iterator(); iterator.hasNext();){
	        	 AssignmentStudent ass_stud = (AssignmentStudent) iterator.next(); 
	             
	         } */
	         
	        
	         tx.commit();
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
	      return submitted_assignment;
	   }
}
